class RoleinfoPanel extends BasePanel {

    //玩家头像
    private head: HeadIcon;
    private btn_renzheng: eui.Button;
    private btn_distance: ButtonEx;
    private lab_name: eui.Label;
    private lab_other: eui.Label;
    private lab_id: eui.Label;
    private lab_ip: eui.Label;
    private lab_good: eui.Label;
    private lab_bad: eui.Label;

    private player: PlayerVo;

    private autoOpen: boolean = false;

    public constructor() {
        super();

        this.skinName = "RoleinfoPanelSkin";
    }

    childrenCreated() {
        super.childrenCreated();

        this.btn_renzheng.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_distance.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);

        gameManager.i.addEventListener(GameEventType.UPDATE_LOCATION, this.openDistance, this);
        gameManager.i.addEventListener(GameEventType.UPDATE_PLAYER_INFO, this.updatePlayerInfo, this);
    }

    private openDistance() {
        if (this.autoOpen && this.visible && this.btn_distance.visible) {
            this.autoOpen = false;
            gameManager.open('DistancePanel');
        }
    }

    private clickHandler(e: egret.TouchEvent) {
        switch (e.currentTarget) {
            case this.btn_renzheng:
                if (this.player.uid == game.player.uid && GameConfig.switch_shoujirenzheng) {
                    this.hide();
                    gameManager.open('TelphonePanel');
                }
                break;
            case this.btn_distance:
                if (!game.poswx) {
                    this.autoOpen = true;
                    Weixin.getLocation();
                }
                else {
                    gameManager.open('DistancePanel');
                }
                break;
        }
    }

    refreshRole(player: any = null, showDisBtn: boolean = false): void {
        if (!player) player = game.player;

        this.player = player;

        this.head.update(player);

        this.btn_distance.visible = GameConfig.switch_distance && showDisBtn;

        this.updatePlayerInfo();
    }

    updatePlayerInfo() {
        this.lab_name.text = "" + this.player.nick;
        this.lab_other.text = "游戏次数：" + this.player.game_times + "  掉线率：" + this.player.drop_rate + "%";
        this.lab_id.text = "I D：" + this.player.uid;
        this.lab_ip.text = GameConfig.switch_sameip ? "I P：" + (this.player.ip == 'unknown' ? '暂不可用' : this.player.ip) : "";
        this.lab_good.text = "被赞 " + this.player.zan;
        this.lab_bad.text = "被踩 " + this.player.cai;
    }
}